package day0302;

/**
 * 单例模式:
 * 在应用中只有一个实例
 */
public class Demo07 {

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println(Singleton.getInstance());
        }
    }
}

// 饿汉
class Singleton {

    private String field;

    private static final Singleton INSTANCE = new Singleton();

    private Singleton() {
    }

    public static Singleton getInstance() {
        return INSTANCE;
    }
}

// 懒汉
class LazySingleton {

    private static LazySingleton INSTANCE;

    private LazySingleton() {
    }

    public static LazySingleton getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new LazySingleton();
        }
        return INSTANCE;
    }

}

// 静态内部类
class InnerSingleton {

    private InnerSingleton(){
    }


    private static class Instance {
        private static final InnerSingleton INSTANCE = new InnerSingleton();
    }

    public static InnerSingleton getInstance() {
        return Instance.INSTANCE;
    }
}
